package service.Impl;

import com.alibaba.fastjson.JSONObject;
import entity.Chongxiu;
import entity.CurriculumList;
import lombok.extern.slf4j.Slf4j;
import mapper.ChongxiuDao;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import service.ChongxiuService;
import utils.ExcelImportUtils;
import utils.PageObject;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * (CurriculumList)表服务实现类
 *
 * @author makejava
 * @since 2021-12-13 16:02:32
 */
@Service("ChongxiuService")
@Slf4j
class ChongxiuServiceImpl implements ChongxiuService {
    @Resource
    private ChongxiuDao ChongxiuDao;


    @Override
    public Chongxiu queryById(Integer pkId) {
        return null;
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<Chongxiu> queryAllByLimit(int offset, int limit) {
        return this.ChongxiuDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param insertobj 实例对象
     * @return 实例对象
     */
    @Override
    public JSONObject insert(JSONObject insertobj) {
        JSONObject resultobj = new JSONObject();
        Chongxiu chongxiu = new Chongxiu();
        chongxiu.setZy(insertobj.getString("zy"));
        chongxiu.setXh(insertobj.getString("xh"));
        chongxiu.setName(insertobj.getString("name"));
        chongxiu.setKcbh(insertobj.getString("kcbh"));
        chongxiu.setKcmc(insertobj.getString("kcmc"));
        chongxiu.setKkyx(insertobj.getString("kkyx"));
        chongxiu.setZkxnxq(insertobj.getString("zkxnxq"));
        chongxiu.setBz(insertobj.getString("bz"));


        chongxiu.setPk_Id(insertobj.getInteger("pk_Id"));

        if (chongxiu.getPk_Id() == null || chongxiu.getPk_Id().equals(0)) {
            List<Chongxiu> curriculumLists = ChongxiuDao.queryAll(chongxiu);
            if (curriculumLists.size() > 0) {
                resultobj.put("code", "-1");
                resultobj.put("msg", "已存在此数据,请核对!");
            } else {
                int insert = ChongxiuDao.insert(chongxiu);
                if (insert == 1) {
                    resultobj.put("code", "0");
                    resultobj.put("msg", "数据新增成功!");
                } else {
                    resultobj.put("code", "-1");
                    resultobj.put("msg", "数据新增失败!");
                }
            }
            System.out.println(resultobj.toJSONString());
            return resultobj;
        } else {
            int update = ChongxiuDao.update(chongxiu);
            if (update == 1) {
                resultobj.put("success", true);
                resultobj.put("msg", "修改数据成功");
            } else {
                resultobj.put("success", false);
                resultobj.put("msg", "修改数据失败");
            }
            return resultobj;
        }


    }

    @Override
    public JSONObject update(JSONObject updateobj) {
        return null;
    }

    /**
     * 通过主键删除数据
     *
     * @param pkId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer pkId) {
        return this.ChongxiuDao.deleteById(pkId) > 0;
    }

    /**
     * 查询所以数据并返回
     *
     * @param jsonObject 查询条件
     * @return pageobject类型对象
     */
    @Override
    public PageObject selectAll(JSONObject jsonObject) {
        PageObject pageObject = new PageObject();
        //当前第几页
        Integer page = jsonObject.getInteger("page");
        //每页几条
        Integer limit = jsonObject.getInteger("limit");
        //计算开始位置
        Integer startindex = (page - 1) * limit;
        List<Chongxiu> chongxiu = ChongxiuDao.selectAll(startindex, limit);
        //查询所有数据条数
        Integer selectcount = ChongxiuDao.selectcount();
        pageObject.setPageCount(page);//第几页
        pageObject.setPageSize(limit);//每页几条
        pageObject.setRowCount(selectcount);//总条数
        pageObject.setPageCount(selectcount / limit);//总页数
        pageObject.setRecords(Collections.singletonList(chongxiu));//当前需展示数据
        return pageObject;
    }

    @Override
    public JSONObject excelimport(MultipartFile multipartFile) throws Exception {
        return null;
    }
}
